import time

from DB_CON import DB_con
from common.shopping import shopping


class buy:
    def __init__(self):
        self.lg=shopping()
        self.lg.QT_login("123@qq.com","123456")

    def QT_shopping(self,li):
        newstepli=[]
        stepli=li[4].split("\n")
        for i in stepli:
            newstepli.append(i.split("=")[1])
        wd=self.lg.QT_buy(newstepli)
        result=self.jude(wd,li[-1],newstepli[-1])
        re=""
        if result:
            re="%s pass"%li[0]
            print("%s pass"%li[0])
        else:
            re="%s failed"%li[0]
            print("%s failed"%li[0])
        with open("../TestReport/GUI测试报告.xls","a+") as f:
            f.writelines(re+"\n")


    def jude(self,wd,exp,news):
        result=True
        # try:
        #     try:
        #         str1=wd.find_element_by_xpath("//div[@class=\" p10\"]/div").text
        #         if exp in str1:
        #             print("不选择收货地址：新增失败")
        #         else:
        #             print("不选择收货地址：新增成功")
        #     except:
        #         try:
        #             warn=wd.find_element_by_class_name("invalid-msg").text
        #             if exp in warn:
        #                 print("除地址外不填写其他字段一个参数：新增失败")
        #             else:
        #                 print("除地址外不填写其他字段一个参数：新增成功")
        #         except:
        #             str=DB_con.select_one("select mobile from tiny_address where mobile=%s;"%news)
        #             if str!=None:
        #                 x="添加地址成功"
        #                 print(x)
        #             else:
        #                 x="添加地址失败"
        #                 print(x)
        # except:
        #     print("失败")
        try:
            str=DB_con.select_one("select mobile from tiny_address where mobile=%s;"%news)
            if str!=None:
                x="添加地址成功"
                print(x)
            else:
                x="添加地址失败"
                print(x)
                # result=False
        except:
            print("失败")
            result=False
        return result
